package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.ResponseResult;
import com.itheima.mapper.HotelMapper;
import com.itheima.pojo.po.Hotel;
import com.itheima.pojo.po.PageByName;
import com.itheima.service.HotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HotelServiceImpl extends ServiceImpl<HotelMapper,Hotel> implements HotelService {
    @Autowired
    private HotelMapper hotelMapper;

    //查询
    @Override
    public List<Hotel> findByName(PageByName pageByName) {
//        System.out.println(pageByName.getName());
        LambdaQueryWrapper<Hotel> wrapper = Wrappers.<Hotel> lambdaQuery();

        wrapper.like(Hotel ::getName,pageByName.getName());

        Page<Hotel> selectPage = hotelMapper.selectPage(new Page<>(pageByName.getPage(), pageByName.getSize()), wrapper);

        //分页结果
        List<Hotel> records = selectPage.getRecords();


        return records;
    }

    //删除
    @Override
    public ResponseResult<Boolean> deleteById(Integer id) {
        int i = hotelMapper.deleteById(id);
        if (i > 0) {
            return ResponseResult.okResult(true);
        }else {
            return ResponseResult.errorResult(3,"删除失败");
        }
    }

    //插入
    @Override
    public ResponseResult<Boolean> addHotel(Hotel hotel) {
        int i = hotelMapper.insert(hotel);
        if (i > 0) {
            return ResponseResult.okResult(true);
        }else {
            return ResponseResult.errorResult(3,"插入失败");
        }
    }

    //更新
    @Override
    public ResponseResult<Boolean> updateHotel(Hotel hotel) {
        LambdaUpdateWrapper<Hotel> wrapper = Wrappers.<Hotel>lambdaUpdate();

        wrapper.eq(Hotel ::getId,hotel.getId())
                .set(Hotel ::getName,hotel.getName())
                .set(Hotel ::getPrice,hotel.getPrice());

        int i = hotelMapper.update(null, wrapper);

        if (i > 0) {
            return ResponseResult.okResult();
        }else {
            return ResponseResult.errorResult(3,"更新失败");
        }
    }
}
